package com.zzz.springsecuritystudy.exception;

import com.zzz.springsecuritystudy.entity.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 处理 BadCredentialsException 异常！
     */
    @ExceptionHandler(BadCredentialsException.class)
    public ResponseResult<String> badCredentialsException(BadCredentialsException e){
        // 打印堆栈信息
        String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
        log.error(message);
        return new ResponseResult<>(100,message,null);
    }

    @ExceptionHandler(BadRequestException.class)
    public ResponseResult<String> badRequestException(BadRequestException e){
        // 打印堆栈信息
        String message = "用户被禁用".equals(e.getMessage()) ? "用户被禁用" : e.getMessage();
        log.error(message);
        return new ResponseResult<>(100,message,null);
    }


}
